package com.lut.edu.apm.payCenter.pay;

import com.alibaba.fastjson.JSONObject;
import com.lut.edu.apm.payCenter.pojo.OrderDetail;
import com.lut.edu.apm.payCenter.pojo.UserOrder;
import com.lut.edu.apm.payCenter.services.OrderDetailService;
import com.lut.edu.apm.payCenter.services.UserOrderService;
import com.lut.edu.apm.webCenter.pojo.UserInfo;
import com.lut.edu.apm.webCenter.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单查询接口
 *
 * @author Zhao Suzhen
 * @date `2024/5/29`
 */
@RestController
@RequestMapping("/queryOrder")
public class OrderQueryController {

    @Autowired
    OrderDetailService orderDetailService;

    @Autowired
    UserOrderService userOrderService;

    @Autowired
    UserInfoService userInfoService;
    /**
     * 查询热销商品
     */
    @GetMapping("queryHotSelling")
    public String queryHotSelling() {
        return JSONObject.toJSONString(orderDetailService.queryHotSelling());
    }

    /**
     * 查询热销商品类型
     */
    @GetMapping("queryTypeCount")
    public String queryTypeCount() {
        List<Map<String, Object>> data = orderDetailService.queryTypeCount();
        Map<String, Object> result = new HashMap<>();
        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();
        for (Map<String, Object> item : data) {
            xAxis.add((String) item.get("product_item"));
            yAxis.add(Long.parseLong(item.get("number").toString()));
        }
        result.put("xAxis", xAxis);
        result.put("yAxis", yAxis);
        return JSONObject.toJSONString(result);
    }

    /**
     * 查询某个时间后的用户订单
     *
     * @param userid 用户id
     * @param time   查询时间
     * @return
     */
    @PostMapping("queryUserOrder")
    @ResponseBody
    public String queryUserOrder(@RequestParam("userId") String userid, @RequestParam("time") String time) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime localDateTime = LocalDateTime.parse(time, formatter);
        List<UserOrder> userOrders = userOrderService.queryUserOrder(userid, localDateTime);
        Map<String, Object> result = new HashMap<>();
        result.put("isSuccess", 401);
        List<Map<String, Object>> resultData = new ArrayList<>();
        for (UserOrder userOrder : userOrders) {
            String title = "";
            String status = "";
            String statusFlag = "";
            if (1 == userOrder.getStatus()) {
                title = userOrder.getGmtCreated().toString();
                status = "已付款";
                statusFlag = "success";
            } else if (0 == userOrder.getStatus()) {
                title = userOrder.getGmtCreated().toString();
                status = "未付款";
                statusFlag = "info";
            } else if (-1 == userOrder.getStatus()) {
                title = userOrder.getGmtCreated().toString() +
                        userOrder.getGmtModified().toString();
                status = "已退款";
                statusFlag = "danger";
            } else {
                title = userOrder.getGmtCreated().toString();
                status = "未知状态";
                statusFlag = "warning";
            }
            Map<String, Object> order = new HashMap<>();
            order.put("title", title);
            order.put("status", status);
            order.put("statusFlag", statusFlag);
            order.put("priceProduct", userOrder.getTotalFee() / 100.0);
            order.put("preferentialPrices", -userOrder.getDeFee() / 100.0);
            order.put("totalFee", userOrder.getReFee() / 100.0);
            List<OrderDetail> orderDetails = orderDetailService.queryOrderDetail(userOrder.getOrderNo());
            List<Map<String, Object>> relOrderDetails = new ArrayList<>();
            for (OrderDetail orderDetail : orderDetails) {
                Map<String, Object> item = new HashMap<>();
                item.put("productName", orderDetail.getProductName());
                item.put("count", orderDetail.getNumber());
                item.put("priceProduct", orderDetail.getPriceProduct() / 100.0);
                item.put("preferentialPrices", orderDetail.getPreferentialPrices() / 100.0);
                item.put("totalFee", orderDetail.getTotalFee() / 100.0);
                relOrderDetails.add(item);
            }
            order.put("tableData", relOrderDetails);
            resultData.add(order);

        }
        UserInfo userInfo = userInfoService.queryInfoByUserId(userid);
        result.put("data", resultData);
        result.put("isSuccess", 200);
        result.put("sex", userInfo.getSex());
        result.put("userPoints", userInfo.getUserPoints() / 100.0);
        return JSONObject.toJSONString(result);
    }
}
